<template>
    <div class="login-box">
        <div class="center">
            <!-- 头像区域 -->
            <div class="avatar">
                <img src="../assets/heima.png" alt="">
            </div>
            <!-- 表单区域  表单-->
            <el-form class="form-box" :model="loginForm" :rules="FromRules" ref="loginFormRef">
              <el-form-item prop="username">
                <el-input  prefix-icon="iconfont icon-user" v-model="loginForm.username">
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input  prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
              </el-form-item>
              <el-form-item class="btns">
                  <el-button type="primary" @click="login">登录</el-button>
                  <el-button type="info" @click="resetLoginForm">重置</el-button>
              </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //    变单验证规则
      FromRules: {
        // 名字需要跟上面数据名一样
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //    重置表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.axios.post('/login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        //  页面跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less">
.login-box {
    position: relative;
    width: 100%;
    height: 100%;
    background: url('../assets/car.jpg');
    background-size: cover;
}
.center {
    width: 450px;
    height: 300px;
    background-color: rgba(0, 0, 0, 0.4);
    border-radius:8px;
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
.avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    box-shadow: 0px 0px 3px 5px black;
    position: absolute;
    /* top: -25%; */
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    padding: 10px;
}
.avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    // margin: 5px;
}
.form-box {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    .btns {
        display: flex;
        justify-content: flex-end;
    }
}
</style>
